package com.lintf.devdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;

/**
 * @ system:Window
 * @ author:lin tingfeng
 * @ data:2022/09/29-15:35
 * @ package_name:com.lintf.devdemo.config
 * @ project_name:DevDemo
 * @ desc:Swagger接口文档配置文件
 */
@Configuration // 配置类
@EnableOpenApi // 开启Swagger3的自动配置
public class SwaggerConfig {

    //配置docket以配置Swagger具体参数
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30) // swagger3文档类型
                .apiInfo(apiInfo())
                .enable(true) //配置是否启用Swagger，如果是false，在浏览器将无法访问
                .select() // 通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()) // 配置如何通过path过滤,即这里只扫描请求以/index开头的接口
                .build()
                //        swagger3 lameda表达式配置全局token
                .securitySchemes(Collections.singletonList(HttpAuthenticationScheme.JWT_BEARER_BUILDER
                        .name("JWT")
                        .build()))
                .securityContexts(Collections.singletonList(SecurityContext.builder()
                        .securityReferences(Collections.singletonList(SecurityReference.builder()
                                .scopes(new AuthorizationScope[0])
                                .reference("JWT")
                                .build()))
                        // 声明作用域
                        .operationSelector(o -> o.requestMappingPattern().matches("/.*"))
                        .build()));
    }

    //配置文档信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("LinTingFeng", "http://www.lintingfeng.top/", "lintingfeng2022@163.com");
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("更多请咨询服务开发者LinTingFeng")
                .contact(contact)
                .version("v1.0")
                .build();
    }
}



